// 切换模态框显示或隐藏
function add(obj, param) {
    $(param.titleId).html(obj.innerText);

    // 初始化表单数据
    initForm(param.formId);

    $(param.modelId).modal("toggle");
}

function copySql(param) {
    let Url2 = $(param.sqlId).html();
    let oInput = document.createElement('input');
    oInput.value = Url2;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    oInput.className = 'oInput';
    oInput.style.display = 'none';
    oInput.remove();
    alert('复制成功');

}

function compare(param) {
    let leftV = $("#" + param.leftId).val();
    let rightV = $("#" + param.rightId).val();

    if (leftV === '' || rightV === '') {
        alert("比对ID不能为空！");
        return;
    }

    $(param.sqlId).html("比对中，请稍等……");

    $.post("/api/tellDifference",{leftId:leftV, rightId: rightV},function (data) {
        console.log(data);
        let model = data.model;
        let sql = "";
        if (model.length === 0) {
            $(param.sqlId).html("没有差异");
        }
        for (let i = 0; i < model.length; i++) {
            sql += model[i] + "<br/>";
        }
        $(param.sqlId).html(sql);
    });

    $(param.resultId).modal("toggle");
}

function deleteDetail(id) {
    let param={id: id};
    $.post("/detail/delete", param, function (data) {
        alert(data.message);
        initTableList();
    });
}


function update(obj, param) {
    $(param.titleId).html(obj.innerText);

    // 初始化表单数据

    $(param.modelId).modal("toggle");
}

function initForm(id) {
    // 清空数据
    let inputList = document.querySelector(id).querySelectorAll("input");
    for (let i = 0; i < inputList.length; i++) {
        inputList.item(i).value = '';
    }
    // 初始化分组数据
    $.post("/sort/list", null, function (data) {
        let model = data.model;
        let text = '<option value="">请选择分组</option>';
        if (model.length > 0) {
            for (let i = 0; i < model.length; i++) {
                text += '<option value="' + model[i].id + '">' + model[i].sortName + '</option>';
            }
        }
        $("#db_group_id").html(text);
    });
}

function initTableList() {
    // 初始化列表
    let url = "/detail/list";
    $("#dataTable").html('');
    $.post(url, null, function (data) {
        let model = data.model;
        let html = "";
        for (let i = 0; i < model.length; i++) {
            let ele = model[i];
            html += "<tr>";
            html += "<th scope='row'>" + (i + 1) + "</th>";
            html += "<td>" + ele.id + "</td>";
            html += "<td>" + ele.name + "</td>";
            html += "<td>" + ele.host + "</td>";
            html += "<td>" + ele.port + "</td>";
            html += "<td>" + ele.userName + "</td>";
            html += "<td>" + ele.schemaName + "</td>";
            html += "<td>" + ele.sortName + "</td>";
            html += "<td>" + ele.dateCreated + "</td>";
            html += "<td>"
            html += '<button id="button-add" type="button" class="btn btn-primary" data-toggle="modal" onclick="deleteDetail(' + ele.id + ')">删除</button>';
            html += "</td>";
            html += "</tr>";
        }
        $("#dataTable").html(html);
    });
}

$(document).ready(function () {
    initTableList();
})
